package cn.jacksigxu.min3halla.block.drink;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;

import javax.annotation.Nullable;
import java.util.function.Function;

@SuppressWarnings("deprecation")
public class BasicDrinkBlock extends Block {

    public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;

    protected final Function<BlockState, VoxelShape> shapeFunction;

    public BasicDrinkBlock(VoxelShape shape) {
        super(Properties.of().strength(0.25f));
        this.shapeFunction = state -> shape;
    }

    public BasicDrinkBlock(VoxelShape shapeNS, VoxelShape shapeEW) {
        super(Properties.of().strength(0.25f));
        this.shapeFunction = state -> state.getValue(FACING).getAxis() == Direction.Axis.X ? shapeNS : shapeEW;
    }

    public BasicDrinkBlock(Function<BlockState, VoxelShape> shapeFunction) {
        super(Properties.of().strength(0.25f));
        this.shapeFunction = shapeFunction;
    }

    @Override
    public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) {
        if (pPlayer.isShiftKeyDown() && pPlayer.getItemInHand(pHand).isEmpty()) {
            pLevel.removeBlock(pPos, false);
            pLevel.gameEvent(GameEvent.BLOCK_DESTROY, pPos, GameEvent.Context.of(pPlayer, pState));
            pLevel.playSound(null, pPos, SoundEvents.ITEM_PICKUP, SoundSource.BLOCKS, 1.0F, 0.8F + pLevel.random.nextFloat() * 0.4F);

            var stack = this.getCloneItemStack(pState, pHit, pLevel, pPos, pPlayer);
            if (pPlayer.isCreative() && pPlayer.getInventory().contains(stack)) {
                return InteractionResult.sidedSuccess(pLevel.isClientSide);
            }
            if (!pPlayer.addItem(stack)) {
                ItemEntity itementity = new ItemEntity(pLevel, pPos.getX() + 0.5D, pPos.getY() + 0.5D, pPos.getZ() + 0.5D, stack);
                itementity.setPickUpDelay(40);
                pLevel.addFreshEntity(itementity);
            }
            return InteractionResult.sidedSuccess(pLevel.isClientSide);
        }
        return super.use(pState, pLevel, pPos, pPlayer, pHand, pHit);
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
        pBuilder.add(FACING);
    }

    @Nullable
    @Override
    public BlockState getStateForPlacement(BlockPlaceContext pContext) {
        return this.defaultBlockState().setValue(FACING, pContext.getHorizontalDirection().getOpposite());
    }

    @Override
    public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
        return this.shapeFunction.apply(pState);
    }
}
